import cryptoJs from 'crypto-js';
//export var baseUrl = '//xnh.xnhkfpt.com/vueapi/api'
export var baseUrl='https://xnhloc.xnhkfpt.com/api'
export var phoneReg = /^1[3456789]\d{9}$/
export var emailReg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/
export var idcardReg = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
export var telephoneReg = /^((\+?86)|(\(\+86\)))?(13[012356789][0-9]{8}|15[012356789][0-9]{8}|18[02356789][0-9]{8}|147[0-9]{8}|1349[0-9]{7})$/
export var encryptDesKey = 'QElSkYBDaGZWNbYr'
export var decryptDesKey='REDSV*()'
//DES加密
export const encryptDes = (message) => {
	var keyHex = cryptoJs.enc.Utf8.parse(encryptDesKey)
	var option = {
		mode: cryptoJs.mode.ECB,
		padding: cryptoJs.pad.Pkcs7
	}
	var encrypted = cryptoJs.DES.encrypt(message, keyHex, option)
	return encrypted.ciphertext.toString().toUpperCase()
} 

//DES解密
export function decryptDes(ciphertext){
    var keyHex = cryptoJs.enc.Utf8.parse(decryptDesKey);
    var decrypted = cryptoJs.DES.decrypt({
        ciphertext: cryptoJs.enc.Hex.parse(ciphertext)
    }, keyHex, {
        mode: cryptoJs.mode.ECB,
        padding: cryptoJs.pad.Pkcs7
    });
    var result_value = decrypted.toString(cryptoJs.enc.Utf8);
    return result_value;
}
//富文本格式化
export function formatRichText(html){
  let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){
    match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
    match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
    match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
    return match;
  });
  newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){
    match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
    return match;
  });
  newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');
  return newContent;
}

export function setCookie(name, value, day) {
	var exp = new Date();
	exp.setTime(exp.getTime() + day * 24 * 60 * 60 * 1000);
	document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

//获取cookie
export function getCookie(objName) { //获取指定名称的cookie的值 
	var arrStr = document.cookie.split("; ");
	for(var i = 0; i < arrStr.length; i++) {
		var temp = arrStr[i].split("=");
		if(temp[0] == objName) {
			return decodeURI(temp[1]);
		}
	}
}

function isClass(o) {
	if(o === null) return "Null";
	if(o === undefined) return "Undefined";
	return Object.prototype.toString.call(o).slice(8, -1);
}

//深度克隆
export function deepClone(obj) {
	if(!obj) {
		return null
	}
	var result, oClass = isClass(obj);
	//确定result的类型
	if(oClass === "Object") {
		result = {};
	} else if(oClass === "Array") {
		result = [];
	} else {
		return obj;
	}

	for(var key in obj) {
		if(obj.hasOwnProperty(key)) {
			var copy = obj[key];
			if(isClass(copy) == "Object") {
				result[key] = deepClone(copy); //递归调用
			} else if(isClass(copy) == "Array") {
				result[key] = deepClone(copy);
			} else {
				result[key] = obj[key];
			}
		}
	}
	return result;
}

//校验是否为手机或固话号码
export function checkNumber(numStr){
	var isTel =
		/(^(0\d{2})-(\d{8})$)|(^(0\d{3})-(\d{7})$)|(^(0\d{2})-(\d{8})-(\d+)$)|(^(0\d{3})-(\d{7})-(\d+)$)|(^\d{7}$)|(^\d{8}$)/
	var isChinaMobile =
		/^134[0-8]\d{7}$|^(?:13[5-9]|147|15[0-27-9]|178|1703|1705|1706|18[2-478])\d{7,8}$/; //移动
	var isChinaUniom = /^(?:13[0-2]|145|15[56]|176|1704|1707|1708|1709|171|18[56])\d{7,8}$/; //联通
	var isChinaTelcom = /^(?:133|153|1700|1701|1702|177|173|18[019])\d{7,8}$/; //电信
	return isTel.test(numStr)||isChinaMobile.test(numStr)||isChinaUniom.test(numStr)||isChinaTelcom.test(numStr)
}

//下载保存文件
export function saveFile(fileUrl) {
	uni.downloadFile({
		url: fileUrl, //仅为示例，并非真实的资源
		success: (res) => {
			if (res.statusCode === 200) {
				var tempFilePath=res.tempFilePath
				uni.openDocument({
				      filePath: tempFilePath,
				      showMenu: true,
				      success: function (res) {
				        console.log('打开文档成功');
				      }
				    });
				// uni.saveFile({
				// 	tempFilePath: tempFilePath,
				// 	success: function (res) {
				// 		console.log(res.savedFilePath)
				// 	}
				// });
			}
		}
	});
}

Date.prototype.Format = function(fmt) { //author: meizz 
	var o = {
		"M+": this.getMonth() + 1, //月份 
		"d+": this.getDate(), //日 
		"h+": this.getHours(), //小时 
		"m+": this.getMinutes(), //分 
		"s+": this.getSeconds(), //秒 
		"q+": Math.floor((this.getMonth() + 3) / 3), //季度 
		"S": this.getMilliseconds() //毫秒 
	};
	if(/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for(var k in o)
		if(new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}